import constants from "./constants";
import {ChatStatusEnum} from "../types/index";
import api from '../api/index'
import {formatTime} from "../utils/util";

const common = {
    getLocalToken: (): string => {
        let token = ''
        try {
            token = wx.getStorageSync(constants.LocalTokenKey) || ''
        } catch (e) {
        }
        return token
    },
    setLocalToken: (token: string) => {
        try {
            wx.setStorageSync(constants.LocalTokenKey, token)
        } catch (e) {
        }
    },
    removeLocalToken: () => {
        try {
            wx.removeStorageSync(constants.LocalTokenKey)
        } catch (e) {
        }
    },
    getLocalTopicId: (uid: string): string => {
        let topicId = ''
        try {
            topicId = wx.getStorageSync(constants.LocalTopicKey + uid) || ''
        } catch (e) {
        }
        return topicId
    },
    setLocalTopicId: (uid: string, topicId: string) => {
        try {
            wx.setStorageSync(constants.LocalTopicKey + uid, topicId)
        } catch (e) {
        }
    },
    removeLocalTopicId: (uid: string) => {
        try {
            wx.removeStorageSync(constants.LocalTopicKey + uid)
        } catch (e) {
        }
    },
    getLocalUid: (): string => {
        let uid = ''
        try {
            uid = wx.getStorageSync(constants.LocalUidKey) || ''
        } catch (e) {
        }
        return uid
    },
    setLocalUid: (uid: string) => {
        try {
            wx.setStorageSync(constants.LocalUidKey, uid+'')
        } catch (e) {
        }
    },
    removeLocalUid: () => {
        try {
            wx.removeStorageSync(constants.LocalUidKey)
        } catch (e) {
        }
    },
    getLocalUser: (): any => {
        let user = {
            uid: ''
        }
        try {
            user = wx.getStorageSync(constants.LocalUserKey) || {uid: ''}
        } catch (e) {
        }
        return user
    },
    setLocalUser: (user: any) => {
        if (!user) {
            return
        }
        try {
            wx.setStorageSync(constants.LocalUserKey, user)
        } catch (e) {
        }
    },
    removeLocalUser: () => {
        try {
            wx.removeStorageSync(constants.LocalUserKey)
        } catch (e) {
        }
    },
    getLocalVipInfo: (): any => {
        let vipInfo = {}
        try {
            vipInfo = wx.getStorageSync(constants.LocalVipKey) || {}
        } catch (e) {
        }
        return vipInfo
    },
    setLocalVipInfo: (vipInfo: any) => {
        if (!vipInfo) {
            return
        }
        try {
            wx.setStorageSync(constants.LocalVipKey, vipInfo)
        } catch (e) {
        }
    },
    removeLocalVipInfo: () => {
        try {
            wx.removeStorageSync(constants.LocalVipKey)
        } catch (e) {
        }
    },
    getAppUserInfo: () => {
        const app = getApp()
        const user = app.globalData.user
        if (user) {
            return user
        } else {
            return common.getLocalUser()
        }
    },
    setAppUserInfo: (userInfo: any, globalData?: any) => {
        if (globalData) {
            globalData.user = userInfo
        } else {
            const app = getApp()
            app.globalData.user = userInfo
        }
        common.setLocalUser(userInfo)
    },
    getAppVipInfo: () => {
        const app = getApp()
        const vipInfo = app.globalData.vipInfo
        if (vipInfo) {
            return vipInfo
        } else {
            return common.getLocalVipInfo()
        }
    },
    setAppVipInfo: (vipInfo: any, globalData?: any) => {
        if (globalData) {
            globalData.vipInfo = vipInfo
        } else {
            const app = getApp()
            app.globalData.vipInfo = vipInfo
        }
        common.setLocalVipInfo(vipInfo)
    },
    getDefChatItem: () => {
        return {
            dialogue_id: '',
            user: '',
            reasoning: '',
            aispire: '',
            answerKey:'answer',
            loading: true,
            reasoningExpand: true,
            finish: false,
            status: ChatStatusEnum.Start,
            userEdit: false,
            editText: '',
            reasoningFinish: false,
            topicId: '',
            similarQuestionList: [],
            referenceList: [],
            referenceListObj: {},
            reasoningObj: {},
            aispireObj: {},
        }
    },
    checkUserQuota: async () => {
        const user = common.getAppUserInfo()
        const uid = user.uid
        let result = {
            r1: true,
            v3: true,
            my_kb: true,
            aispire_kb: true,
            online: true,
            files: true,
        }
        try {
            const res: any = await api.checkUserQuota({
                uid,
            })
            if (res.high_model_exceeded === true) {
                result.r1 = false
            }
            if (res.regular_model_exceeded === true) {
                result.v3 = false
            }
            if (res.my_knowledge_base_exceeded === true) {
                result.my_kb = false
            }
            if (res.aispire_knowledge_base_exceeded === true) {
                result.aispire_kb = false
            }
            if (res.online_search_exceeded === true) {
                result.online = false
            }
            if (res.file_upload_exceeded === true) {
                result.files = false
            }
        } catch(e){

        }
        return result
    },
    getCheckMsg: async (params: any, allModel?: boolean) => {
        let msg = ''
        const result = await common.checkUserQuota()
        if (allModel) {
            if (!result.v3) {
                msg = '模型今日次数已全部用完，升级会员享受更高用量'
            } else if (params.knowledge_base && params.knowledge_base.includes('private_knowledge_base') && !result.my_kb) {
                msg = '【搜我的智库】今日次数已全部用完，升级会员享受更高用量或取消【搜我的智库】'
            } else if (params.knowledge_base && params.knowledge_base.includes('aispire_knowledge_base') && !result.aispire_kb) {
                msg = '【搜AISPIRE】今日次数已全部用完，升级会员享受更高用量或取消【搜AISPIRE】'
            } else if (params.knowledge_base && params.knowledge_base.includes('online_search') && !result.online) {
                msg = '【搜全网】今日次数已全部用完，升级会员享受更高用量或取消【搜全网】'
            } else if (params.files && params.files.length && !result.files) {
                msg = '【附件解析】今日次数已全部用完，升级会员享受更高用量或删除附件'
            }
        } else {
            if (!result.v3) {
                msg = '模型今日次数已全部用完，升级会员享受更高用量'
            } else if (params.knowledge_base && params.knowledge_base.includes('private_knowledge_base') && !result.my_kb) {
                msg = '【搜我的智库】今日次数已全部用完，升级会员享受更高用量或取消【搜我的智库】'
            } else if (params.knowledge_base && params.knowledge_base.includes('aispire_knowledge_base') && !result.aispire_kb) {
                msg = '【搜AISPIRE】今日次数已全部用完，升级会员享受更高用量或取消【搜AISPIRE】'
            } else if (params.knowledge_base && params.knowledge_base.includes('online_search') && !result.online) {
                msg = '【搜全网】今日次数已全部用完，升级会员享受更高用量或取消【搜全网】'
            } else if (params.files && params.files.length && !result.files) {
                msg = '【附件解析】今日次数已全部用完，升级会员享受更高用量或删除附件'
            }
        }
        return msg
    },
    getVipInfo: async () => {
        let vipInfo = {
            levelDesc: '',
            levelName: '',
            status: 0,
            endTime: '',
        }
        try {
            const res = await api.getVipInfo()
            if (res.code === 401) {
                // wx.reLaunch({
                //     url: '/pages/login/login?single=true'
                // })
                return null
            } else {
                if (res.data) {
                    vipInfo.levelDesc = res.data.levelDesc
                    vipInfo.levelName = res.data.levelName
                    vipInfo.status = res.data.currentMemberStatus
                    vipInfo.endTime = res.data.endTime ? formatTime(res.data.endTime) : ''
                }
            }
        } catch(e){

        }
        return vipInfo
    },
    getUserStorageInfo: async (uid: string) => {
        const storageInfo = {
            use: 0,
            total: 0,
            percent: 0,
        }
        try {
            const res = await api.getUserStorageInfo({uid})
            if (res) {
                try {
                    storageInfo.use = res.use_capacity.toFixed(2)
                    storageInfo.total = res.storage_capacity.toFixed(2)
                    storageInfo.percent = res.usage_percentage > 100 ? 100 : res.usage_percentage
                } catch(e){
                    console.log(e)
                }
            }
        } catch(e){

        }
        return storageInfo
    }
}
export default common
